package com.jackiechancinema.ssm.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.dao.FileHomeDao;
import com.jackiechancinema.ssm.dto.FileHomeExcelDto;
import com.jackiechancinema.ssm.entity.FileHome;
import com.jackiechancinema.ssm.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FileHomeReadListener extends AnalysisEventListener<FileHomeExcelDto> {
    @Autowired
    private FileHomeDao fileHomeDao;

    private List<FileHome> list=new ArrayList<FileHome>();
    //获取读取到的每一行对应的对象
    @Override
    public void invoke(FileHomeExcelDto dto, AnalysisContext analysisContext) {
        //实现属性的赋值
        FileHome fileHome= BeanUtil.copyProperty(FileHome.class,dto,dto.getClass().getDeclaredFields());
        //添加到集合
        list.add(fileHome);
    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        fileHomeDao.insertBatch(list);
        list.clear();
    }
}